log using Replication_log_file, replace 
///////////////////////////////////////////////////////////////////////////////
//////////////////////////// Replication file /////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

// The following analyses were carried out using Stata/SE 15.1

// This script constructs the analysis dataset and reproduces the results in 
// the paper "Does Deliberative Education Increase Civic Competence?
// Results from a Field Experiment"

// Uncomment the following line if you have not installed the "balancetable" command
// ssc install balancetable
// Uncomment the following line if you have not installed the "ritest" command
// net install ritest

set more off
clear

// Setting the working directory.
// Note: The directory need to include the datasets "Deliberation_Data.dta" and
// "Deliberation_Grades_Data.dta" and one folder named "Tables".

cd "C:/Users/xlunsi/Dropbox/VR utb reformer/DataAndPaper"

// Load data
use  "Deliberation_Data.dta", clear


///// Preparation of the data /////

// Recoding outcome variables and creating indices

// Outcome variable: Interest

// Wave 1
recode Samhallsintresse (1=1) (2=.666) (3=.333) (4=0)  (5/999=.), gen(interest1)

// Wave 2
recode Samhallsintresse2 (1=1) (2=.666) (3=.333) (4=0)  (5/999=.), gen(interest2)

// Wave 3
recode Samhallsintresse3 (1=1) (2=.666) (3=.333) (4=0)  (5/999=.), gen(interest3)


// Outcome variable: Values

// Wave 1
gen values1 = values1a  + values1c + values1d + values1e + values1f + values1g  + values1i + values1j 
replace values1 = (values1 - 8 ) / 24

// Wave 2
gen values2 = values2a  + values2c + values2d + values2e + values2f + values2g  + values2i + values2j 
replace values2 = (values2 - 8 ) / 24
alpha values2a    values2c  values2d  values2e  values2f  values2g    values2i  values2j     

// Wave 3
gen values3 = values3a  + values3c + values3d + values3e + values3f + values3g  + values3i + values3j 
replace values3 = (values3 - 8 ) / 24
alpha values3a    values3c  values3d  values3e  values3f  values3g    values3i  values3j     

// Outcome varaible: Discussions

// Wave 1
gen talk1 = talk1a + talk1b + talk1c 
replace talk1 = (talk1 - 3 ) / 9

// Wave 2
gen talk2 = talk2a + talk2b + talk2c 
replace talk2 = (talk2 - 3 ) / 9
alpha talk2a  talk2b  talk2c 

// Wave 3
gen talk3 = talk3a + talk3b + talk3c 
replace talk3 = (talk3 - 3 ) / 9
alpha  talk3a talk3b talk3c 

// Outcome variable: Knowledge

// Wave 1
gen knowledge1 = knowledge1a + knowledge1b + knowledge1c + knowledge1d + knowledge1e
replace knowledge1 =. if knowledge1a == . & knowledge1b == . & knowledge1c ==. ///
& knowledge1d == . & knowledge1e == . 

// Wave 2
gen knowledge2 =  knowledge2a + knowledge2b + knowledge2c + knowledge2d + ///
knowledge2e + knowledge2f + knowledge2g
alpha  knowledge2a  knowledge2b  knowledge2c  knowledge2d knowledge2e ///
knowledge2f knowledge2g
replace knowledge2  =.  if knowledge2a == . & knowledge2b == . & knowledge2c ==. ///
& knowledge2d == . & knowledge2e == .  & knowledge2f == . & knowledge2g == . 

// Wave 3
gen knowledge3 =  knowledge3a + knowledge3b + knowledge3c + knowledge3d + ///
knowledge3e + knowledge3f + knowledge3g
replace knowledge3 =.  if knowledge3a == . & knowledge3b == . & knowledge3c ==. ///
& knowledge3d == . & knowledge3e == .  & knowledge3f == . & knowledge3g == . 
alpha knowledge3a  knowledge3b  knowledge3c knowledge3d knowledge3e knowledge3f knowledge3g


// Outcome variable: Knowledge, alternative coding

// Wave 1
egen xknowledge1=rowtotal(xknowledge1a  xknowledge1b xknowledge1c xknowledge1d xknowledge1e)
replace xknowledge1  =. if knowledge1a == . & knowledge1b == . & knowledge1c ==. ///
& knowledge1d == . & knowledge1e == . 

// Wave 2
egen xknowledge2 = rowtotal(xknowledge2a  xknowledge2b  xknowledge2c ///
xknowledge2d  xknowledge2e  xknowledge2f  xknowledge2g)
replace xknowledge2  =.  if knowledge2a == . & knowledge2b == . & knowledge2c ==. ///
& knowledge2d == . & knowledge2e == .  & knowledge2f == . & knowledge2g == . 
alpha  xknowledge2a  xknowledge2b  xknowledge2c  xknowledge2d xknowledge2e ///
xknowledge2f xknowledge2g

// Wave 3
egen xknowledge3 = rowtotal(xknowledge3a  xknowledge3b  xknowledge3c ///
 xknowledge3d  xknowledge3e  xknowledge3f  xknowledge3g)
replace xknowledge3   =.  if knowledge3a == . & knowledge3b == . & knowledge3c ==. ///
& knowledge3d == . & knowledge3e == .  & knowledge3f == . & knowledge3g == .
alpha xknowledge3a  xknowledge3b  xknowledge3c xknowledge3d ///
 xknowledge3e xknowledge3f xknowledge3g

// Create cimate indicator used for table A3 in appendix 4
gen climate = climateQ1 + climateQ2 + climateQ3 + climateQ4 + climateQ5 + climateQ6 ///
+ climateQ + climateQ8 + climateQ9 + climateQ10 + climateQ11

replace climate = (climate -11) /33


// Label variables
label variable values2 "Values"
label variable interest2 "Interest"
label variable knowledge2 "Knowledge"
label variable xknowledge2 "Knowledge alternative coding"
label variable talk2 "Discussions" 
label variable values3 "Values"
label variable interest3 "Interest"
label variable knowledge3 "Knowledge"
label variable xknowledge3 "Knowledge alternative coding"
label variable talk3 "Discussions"
label variable climate "Classroom climate Index"


////// Analysis of the data //////


// Reconstructing table 1 in main paper //

// OLS model with baseline level of outcome and other control variables included 
// Directly after experiment 
	foreach k in interest values knowledge talk   {
 eststo: reg `k'2 t  `k'1 books i.mother i.father female  , vce(cl classID)
gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
}
esttab using "Tables\mainpaper1.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers 
 	eststo clear


// Reconstructing table 2 in main paper //

// OLS model with baseline level of outcome and other control variables included 
// End of the year
	foreach k in interest values knowledge talk   {
 eststo: reg `k'3 t  `k'1 books i.mother i.father female  , vce(cl classID)
gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
}
esttab using "Tables\mainpaper2.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers  
 	eststo clear

// Reconstructing table A1 in appendix 2 //

// Randomization checks for treated vs un untreated classes
ta mother, gen(mother)
ta books, gen(books)
ta father, gen(father)
preserve
collapse (mean) t mother1-mother4 father1-father4 books1-books4 female , by(classID)
balancetable t female  books1-books4 mother1-mother4 father1-father4 using "Tables\rando.tex",replace
restore 


// Reconstructing table A3 in appendix 4 //

// Manipulation check 
eststo clear
eststo: qui reg climate  t, vce(cl classID)
esttab using "Tables\manipCheck.tex"  , label se         ///
     varwidth(13) b(3)  nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("Manipulation checks")  nonumbers
eststo clear
alpha climateQ1 climateQ2 climateQ3 climateQ4 climateQ5 climateQ6 ///
climateQ climateQ8 climateQ9 climateQ10 climateQ11


// Reconstructing table A5 in appendix 7 //

// OLS models i) unadjusted, ii) adjusted for baseline level of outcome and
// iii) adjusted for baseline level of outcome & other control variables included.

// Directly after experiment, Interest & Values
	foreach k in interest values  {
	 eststo: reg `k'2 t    , vce(cl classID)
	 gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'

 eststo: reg `k'2 t  `k'1   , vce(cl classID)
 gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'

 eststo: reg `k'2 t  `k'1 books i.mother i.father female  , vce(cl classID)
  gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
}
esttab using "Tables\reg1.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers 
 	eststo clear
	

// Reconstructing table A6 in appendix 7 //

// Directly after experiment, Knowledge & Discussions
	foreach k in knowledge talk  {
	 eststo: reg `k'2 t    , vce(cl classID)
	   gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
 eststo: reg `k'2 t  `k'1   , vce(cl classID)
   gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
 eststo: reg `k'2 t  `k'1 books i.mother i.father female  , vce(cl classID)
gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
}
esttab using "Tables\reg2.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers 
 	eststo clear


// Reconstructing table A7 in appendix 7 //

// End of the year, Interest & Values
		foreach k in interest values  {
	 eststo: reg `k'3 t    , vce(cl classID)
	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
 eststo: reg `k'3 t  `k'1   , vce(cl classID)
 	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
 eststo: reg `k'3 t  `k'1 books i.mother i.father female  , vce(cl classID)
	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k' 

}
esttab using "Tables\reg3.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers 
 	eststo clear
	

// Reconstructing table A8 in appendix 7 //

// End of the year, Knowledge & Discussions
	foreach k in knowledge talk  {
	 eststo: reg `k'3 t    , vce(cl classID)
	 	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
 eststo: reg `k'3 t  `k'1   , vce(cl classID)
 	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
 eststo: reg `k'3 t  `k'1 books i.mother i.father female  , vce(cl classID)
 	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'

}
esttab using "Tables\reg4.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers  
 	eststo clear


// Reconstructing table A9 in appendix 8 //

// Multilevel random-intercept models i) unadjusted, ii) adjusted for baseline
// level of outcome and iii) adjusted for baseline level of outcome & other
// control variables included.

// Directly after experiment, Interest & Values
	foreach k in interest values {
	 eststo: xtmixed `k'2 t    || classID : , var
	  	 gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
 eststo: xtmixed `k'2 t  `k'1   || classID : , var
  	 gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
 eststo: xtmixed `k'2 t  `k'1 books i.mother i.father female  || classID : , var
  	 gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
}
esttab using "Tables\mixed1_1.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers ///
	 transform(ln*: exp(@) exp(@)) ///
    eqlabels("" "sd(Intercept at level 2)" "sd(Residuals at level 1)" "sd(Residual)", none) ///
 varlabels(,elist(weight:_cons "{break}{hline @width}"))  
	eststo clear


// Reconstructing table A10 in appendix 8 //

// Directly after experiment, Knowledge & Discussions
		foreach k in  knowledge talk {
	 eststo: xtmixed `k'2 t    || classID : , var
	   	 gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
 eststo: xtmixed `k'2 t  `k'1   || classID : , var
   	 gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
 eststo: xtmixed `k'2 t  `k'1 books i.mother i.father female  || classID : , var
   	 gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
}
esttab using "Tables\mixed1_2.tex", keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers ///
	 transform(ln*: exp(@) exp(@)) ///
    eqlabels("" "sd(Intercept at level 2)" "sd(Residuals at level 1)" "sd(Residual)", none) ///
 varlabels(,elist(weight:_cons "{break}{hline @width}"))  
	eststo clear
 

// Reconstructing table A11 in appendix 8 //

// End of year, Interest & Values
	foreach k in interest values {
	 eststo: xtmixed `k'3 t    || classID : , var
	 	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
 eststo: xtmixed `k'3 t  `k'1   || classID : , var
 	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
 eststo: xtmixed `k'3 t  `k'1 books i.mother i.father female  || classID : , var
 	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
}
esttab using "Tables\mixed2_1.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers ///
	 transform(ln*: exp(@) exp(@)) ///
    eqlabels("" "sd(Intercept at level 2)" "sd(Residuals at level 1)" "sd(Residual)", none) ///
 varlabels(,elist(weight:_cons "{break}{hline @width}"))  
	eststo clear


// Reconstructing table A12 in appendix 8 //

// End of year, Knowledge & Discussions
		foreach k in  knowledge talk {
	 eststo: xtmixed `k'3 t    || classID : , var
	 	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
 eststo: xtmixed `k'3 t  `k'1   || classID : , var
 	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
 eststo: xtmixed `k'3 t  `k'1 books i.mother i.father female  || classID : , var
 	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
}
esttab using "Tables\mixed2_2.tex", keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers ///
	 transform(ln*: exp(@) exp(@)) ///
    eqlabels("" "sd(Intercept at level 2)" "sd(Residuals at level 1)" "sd(Residual)", none) ///
 varlabels(,elist(weight:_cons "{break}{hline @width}"))  
	eststo clear
	
	
	
// Reconstructing table A13 in appendix 8 //

// Multilevel random-intercept, and random-slopes models adjusted for baseline 
// level of outcome & other control variables included.

// Directly after experiment
	foreach k in interest values knowledge talk {
 eststo: xtmixed `k'2 t  `k'1 books i.mother i.father female  ||   teacherID : t  || classID : 
  	 gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
}
esttab using "Tables\mixedB1.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers ///
	 transform(ln*: exp(@) exp(@)) ///
    eqlabels("" "sd(Intercept at level 2)" "sd(Residuals at level 1)" "sd(Residual)", none) ///
 varlabels(,elist(weight:_cons "{break}{hline @width}"))  
	eststo clear
	

// Reconstructing table A14 in appendix 8 //

// End of year
	foreach k in interest values knowledge talk {
 eststo: xtmixed `k'3 t  `k'1 books i.mother i.father female  ||   teacherID : t  || classID : 
  	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
}
esttab using "Tables\mixedB2.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers ///
	 transform(ln*: exp(@) exp(@)) ///
    eqlabels("" "sd(Intercept at level 2)" "sd(Residuals at level 1)" "sd(Residual)", none) ///
 varlabels(,elist(weight:_cons "{break}{hline @width}"))  
	eststo clear

	
// Reconstructing table A15 in appendix 9 //

// OLS models using alternative coding for knowledge
// i) unadjusted, ii) adjusted for baseline level of outcome and
// iii) adjusted for baseline level of outcome & other control variables included.

// Directly after experiment & end of year 
	foreach k in xknowledge {
	 eststo: reg `k'2 t   , vce(cl classID)
 eststo: reg `k'2 t  `k'1    , vce(cl classID)
 eststo: reg `k'2 t  `k'1 books i.mother i.father female , vce(cl classID)
 	 eststo: reg `k'3 t     , vce(cl classID)
 eststo: reg `k'3 t  `k'1   , vce(cl classID)
 eststo: reg `k'3 t  `k'1 books i.mother i.father female   , vce(cl classID)
}
esttab using "Tables\regxknowledge_.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers 
  eststo clear
  
  
// Reconstructing table A16 in appendix 9 //

// Multilevel random-intercept model using alternative coding for knowledge
// i) unadjusted, ii) adjusted for baseline level of outcome and
// iii) adjusted for baseline level of outcome & other control variables included.

// Directly after experiment & end of year 
  foreach k in xknowledge {
	 eststo: xtmixed `k'2 t    || classID : , var
	   	 gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
 eststo: xtmixed `k'2 t  `k'1   || classID : , var
   	 gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
 eststo: xtmixed `k'2 t  `k'1 books i.mother i.father female  || classID : , var
   	 gen sample1`k' = e(sample)
tabstat `k'2 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
	 eststo: xtmixed `k'3 t    || classID : , var
	   	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
 eststo: xtmixed `k'3 t  `k'1   || classID : , var
   	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
 eststo: xtmixed `k'3 t  `k'1 books i.mother i.father female  || classID : , var
   	 gen sample1`k' = e(sample)
tabstat `k'3 if sample1`k'==1 & t==0, s(mean N) format(%10.3fc)
drop sample1`k'
}
esttab using "Tables\xknowledge_.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers ///
	 transform(ln*: exp(@) exp(@)) ///
       eqlabels("" "sd(Intercept at level 2)" "sd(Residuals at level 1)" "sd(Residual)", none) ///
 varlabels(,elist(weight:_cons "{break}{hline @width}"))  
  eststo clear
 
 
// Reconstructing table A17 in appendix 10 //

// Generate strata indicator for teachers participating with more then 1 class.
qui by teacherID classID , sort: generate nvals = _n == 1
qui by teacherID:  replace nvals = sum(nvals)
qui by teacherID: replace nvals = nvals[_N]
gen strata = teacherID if nvals!=1
replace strata = 99 if nvals==1


// Calculate p-values for permutation tests adjusted for baseline level of outcome & other
// control variables included.

// Directly after experiment
	foreach k in interest values knowledge talk   {
ritest t _b[t], reps(2000) cluster(classID) strata(strata) seed(123): reg `k'2 t  `k'1  books i.mother i.father female    , vce(cl classID)	
}

// End of year
	foreach k in interest values knowledge talk   {
ritest t _b[t], reps(2000) cluster(classID) strata(strata) seed(123): reg `k'3 t  `k'1  books i.mother i.father female    , vce(cl classID)	
}


// Reconstructing table A18 in appendix 11 //

// Generate strata indicators for teachers participating with more then 1 class.
qui tab teacherID if nvals>1, gen(blocks)  
foreach var of varlist blocks1-blocks14     {
replace `var' = 0 if `var' ==.
}
gen blocks0= 1 if nvals<2
replace blocks0=0 if nvals>1


// Saturated regression models using method from Lin (2013). All coavariates, including
// strata indicators are centered around their respective grand mean. The centered coaviates
// are included in the regressions and also interacted with the treatment indicator. 
// Standard error clustered at class level. See appendix 11 for a more detailed discussion. 

// Directly after experiment
	foreach k in interest values knowledge talk   {
	local Z  female father2-father4 mother2-mother4 books blocks1-blocks14
quietly xtmixed `k'2 t `k'1 `Z'   ||   teacherID : t  || classID :
gen sample1`k' = e(sample)
foreach var in `k'1 female father1 father2 father3 father4 mother1 mother2 mother3 mother4 books1 books2 books3 books4 blocks1 blocks2 blocks3 blocks4 blocks5 blocks6 blocks7 blocks8 blocks9 blocks10 blocks11 blocks12 blocks13 blocks14 books {
qui noisily capture drop `var'me 
qui noisily capture drop `var'ce
egen `var'me = mean(cond(sample1`k'==1, `var', .))
gen `var'ce=`var'-`var'me
}
local X  female father2-father4 mother2-mother4 books blocks1-blocks14
eststo: xtmixed `k'2 t `k'1 `X' i.t#c.`k'1ce i.t#c.femalece  i.t#c.father2ce i.t#c.father3ce i.t#c.father4ce  i.t#c.mother2ce i.t#c.mother3ce i.t#c.mother4ce i.t#c.blocks1ce	i.t#c.blocks2ce	i.t#c.blocks3ce	i.t#c.blocks4ce	i.t#c.blocks5ce	i.t#c.blocks6ce	i.t#c.blocks7ce	i.t#c.blocks8ce	i.t#c.blocks9ce	i.t#c.blocks10ce	i.t#c.blocks11ce	i.t#c.blocks12ce	i.t#c.blocks13ce	i.t#c.blocks14ce i.t#c.booksce   ||   teacherID : t  || classID :
qui noisily capture drop sample1`k'
}

esttab using "lin1.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers ///
	 transform(ln*: exp(@) exp(@)) ///
    eqlabels("" "sd(Intercept level at 2)" "sd(Residuals leve at 1)" "sd(Residual)", none) ///
 varlabels(,elist(weight:_cons "{break}{hline @width}"))  
 	eststo clear


// Reconstructing table A19 in appendix 11 //

// End of year, note that block 9 is removed as only one class in that block 
// participated in the final survey. 
	foreach k in interest values knowledge talk   {
	local Z  female father2-father4 mother2-mother4 books blocks1-blocks8 blocks10-blocks14
 quietly xtmixed `k'3 t `k'1 `Z'  ||   teacherID : t  || classID : 
gen sample1`k' = e(sample)
foreach var in `k'1  blocks1 blocks2 blocks3 blocks4 blocks5 blocks6 blocks7 blocks8 blocks9 blocks10 blocks11 blocks12 blocks13 blocks14 female father1 father2 father3 father4 mother1 mother2 mother3 mother4 books1 books2 books3 books4 books {
qui noisily capture drop `var'me `var'ce
egen `var'me = mean(cond(sample1`k'==1, `var', .))
gen `var'ce=`var'-`var'me
}

local X  female father2-father4 mother2-mother4 books blocks1-blocks8 blocks10-blocks14
eststo: xtmixed `k'3 t `k'1ce `X' i.t#c.`k'1ce i.t#c.femalece i.t#c.blocks1ce	i.t#c.blocks2ce	i.t#c.blocks3ce	i.t#c.blocks4ce	i.t#c.blocks5ce	i.t#c.blocks6ce	i.t#c.blocks7ce	i.t#c.blocks8ce	i.t#c.blocks10ce	i.t#c.blocks11ce	i.t#c.blocks12ce	i.t#c.blocks13ce	i.t#c.blocks14ce  i.t#c.father2ce i.t#c.father3ce i.t#c.father4ce  i.t#c.mother2ce i.t#c.mother3ce i.t#c.mother4ce  i.t#c.booksce ||   teacherID : t  || classID : 
qui noisily capture drop sample1`k'
}
esttab using "lin2.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers ///
	 transform(ln*: exp(@) exp(@)) ///
    eqlabels("" "sd(Intercept level at 2)" "sd(Residuals leve at 1)" "sd(Residual)", none) ///
 varlabels(,elist(weight:_cons "{break}{hline @width}"))  
 	eststo clear


// Analysis of the effect on grades //

// Reconstructing table A20 in appendix 12 //

// Collapse data on class level and merge with grade dataset 
bysort classID: egen size=count(studentID)
label define t 0 "Control" 1 "Treatment"
preserve
collapse (mean) interest1 values1 knowledge1 talk1 interest2 values2 knowledge2 talk2 interest3 values3 knowledge3 talk3 size mother1-mother4 father1-father4 books1-books4 books female t, by(classID)
merge m:1 classID using "Deliberation_Grades_Data", gen(merge_grades)


// OLS-models i) unadjusted, ii) adjusted for baseline level of average knowledge
// iii) adjusted for average knowledge & other control variables included.

local X "mother2-mother4 father2-father4 books female"
	foreach k in index1 pass {
	 eststo: reg `k' t, r    
	 	 gen sample1`k' = e(sample)
tabstat `k' if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
 eststo: reg `k' t  `X', r  
  	 gen sample1`k' = e(sample)
tabstat `k' if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k'
 eststo: reg `k' t  `X' knowledge1, r 
  	 gen sample1`k' = e(sample)
tabstat `k' if sample1`k'==1 & t==0, s(mean) format(%10.3fc)
drop sample1`k' 
}
esttab using "Tables\grades.tex"  , keep(t _cons) label se  b(3) se(3) obslast ///
     varwidth(13)   nogap  replace star(* 0.10 ** 0.05 *** 0.01) ///
	 title("After exp") nonumbers 
eststo clear 

// Posteriori power analysis referenced in appendix 12.
pcorr index1 t mother2-mother4 father2-father4 books female knowledge1 
pcorr pass t mother2-mother4 father2-father4 books female knowledge1
power pcorr 0.0107, ncontrol(9) alpha(.05)  
power pcorr 0.0250, ncontrol(9) alpha(.05)
restore
log close
